package net.sxnic.ugr.menu.dao;

import java.util.List;

import net.sxinfo.core.dao.Page;
import net.sxinfo.core.dao.hibernate3.HibernateCriteria;
import net.sxinfo.core.spring25.HibernateDao;
import net.sxnic.ugr.menu.Menu;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Query;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

@Repository("menuDao")
public class HibernateMenuDao extends HibernateDao<Menu, String> implements
		MenuDao {

	public List<Menu> getTopLevel() {
		return this
				.find("from Menu where length(code)=2 order by menuOrder,code");
	}

	public List<Menu> getSubLevel(String parentMenuCode) {
		if (StringUtils.isBlank(parentMenuCode)) {
			return null;
		}
		if (parentMenuCode.length() % 2 != 0) {
			return null;
		}

		int len = parentMenuCode.length();

		return this.find("from Menu where length(code)="
				+ String.valueOf(len + 2) + " and code like '" + parentMenuCode
				+ "%' order by menuOrder,code");
	}

	public List<Menu> getAllmenus() {

		return this.find("from Menu where 1=1");
	}

	public List<Menu> getAllByOrder() {

		return this.find("from Menu where status=? order by menuOrder,code",
				new Object[] { Menu.ENABLED_STAUTUS });
	}

	public List<Menu> getAllByLevel() {
		return this
				.find(
						"from Menu where status=?  order by length(code),menuOrder,code",
						new Object[] { Menu.ENABLED_STAUTUS });
	}

	public Page getAllByLevel(int page, int pageSize, String orderProperty,
			boolean asc) {
		HibernateCriteria hc = new HibernateCriteria();

		// 加入默认查询条件
		hc.add(Restrictions.eq("status", Menu.ENABLED_STAUTUS));

		// 加入默认排序条件
		hc.add(Order.asc("menuOrder"));
		hc.add(Order.asc("code"));

		// 如果没有设置排序条件就默认使用日期排序
		if (orderProperty != null) {
			if (asc) {
				hc.add(Order.asc(orderProperty));
			} else {
				hc.add(Order.desc(orderProperty));
			}
		}

		return getPageByCriteria(page, pageSize, hc);
	}

	public Page getAllByOrder(int page, int pageSize, String orderProperty,
			boolean asc) {
		HibernateCriteria hc = new HibernateCriteria();

		// 加入默认查询条件
		hc.add(Restrictions.eq("status", Menu.ENABLED_STAUTUS));

		// 加入默认排序条件
		// hc.add(Order.asc("length(code)"));
		hc.add(Order.asc("menuOrder"));
		hc.add(Order.asc("code"));

		// 如果没有设置排序条件就默认使用日期排序
		if (orderProperty != null) {
			if (asc) {
				hc.add(Order.asc(orderProperty));
			} else {
				hc.add(Order.desc(orderProperty));
			}
		}

		return getPageByCriteria(page, pageSize, hc);
	}

	public void clear() {
		getCurrSession()
				.createQuery("delete Menu").executeUpdate();
	}
	
	public Menu getMenu(String code) {
		return findUnique("from Menu where code = ?", new Object[] { code });
	}

	@Override
	public List<Menu> getTopList() {
		Query query = getCurrSession().createQuery(" from Menu where parent.id is null order by priority asc ");
		return query.list();
	}

	@Override
	public List<Menu> getChildList(String root) {
		Query query = getCurrSession().createQuery("from Menu " + 
				" where parent.id=? order by priority asc, id asc " );
		query.setParameter(0, root);
		return query.list();
	}

}
